package com.bistu.sim.xwy.news.BusinessMode;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bistu.sim.xwy.news.LoginActivity;
import com.bistu.sim.xwy.news.R;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

public class BusinessRegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText busname_register_edit;
    private EditText buspass_register_edit;
    private EditText buspass_register_edit2;
    private Button register_button;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_business_register);
        initView();
    }

    public void initView(){
        busname_register_edit = (EditText) findViewById(R.id.busname_register_edit);
        buspass_register_edit = (EditText) findViewById(R.id.buspass_register_edit);
        buspass_register_edit2 = (EditText) findViewById(R.id.buspass_register_edit2);
        register_button = (Button) findViewById(R.id.register_button);
        register_button.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()){
            case R.id.register_button:
                BusRegister();
                break;
        }
    }

    public void BusRegister(){
        String busname = busname_register_edit.getText().toString().trim();
        String buspass = buspass_register_edit.getText().toString().trim();
        String buspass2 = buspass_register_edit2.getText().toString().trim();
        if(TextUtils.isEmpty(busname)){
            Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(buspass)){
            Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(buspass2)){
            Toast.makeText(this,"请确认密码",Toast.LENGTH_SHORT).show();
            return;
        }
        if(!buspass.equals(buspass2)){
            Toast.makeText(this,"两次密码不一致",Toast.LENGTH_SHORT).show();
            return;
        }

        String url = getString(R.string.serverurl)+"bus/addBus";
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("username",busname);
        params.put("password",buspass);
        client.post(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    Toast.makeText(BusinessRegisterActivity.this,"注册成功，请登录",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(BusinessRegisterActivity.this, LoginActivity.class);
                    startActivity(intent);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Toast.makeText(BusinessRegisterActivity.this,"商户注册失败，请重试",Toast.LENGTH_SHORT).show();
            }
        });
    }

}

